package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询分类信息
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        List<Category> list = this.categoryService.queryListByParent(pid);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    //修改品牌时候，先根据品牌ID来查询分类信息进行回显
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrand(@PathVariable("bid") Long bid) {
        List<Category> list = this.categoryService.queryByBrand(bid);
        if (list == null && list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    //新增分类  每次新增的时候，修改原先的节点为父结点
    @PostMapping("add")
    public ResponseEntity<Void> addCategory(Category category) {
        //添加之前，原先的节点的父结点标示改为1，则才可以展开
        categoryService.addCategory(category);
        categoryService.updateOldCode(category.getParentId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //修改分类
    @PutMapping("edit")
    public ResponseEntity<Void> editCategory(@RequestParam("id") Long id, @RequestParam("name") String name) {
        categoryService.editCategory(id, name);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //删除分类 删除节点的同时，若有下级子节点，则删除父节点的同时连带子节点一起删除
    @DeleteMapping("delete/{id}")
    public ResponseEntity<Void> delCategory(@PathVariable("id") Long id) {
        System.out.println(id);
        categoryService.delCategory(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //根据id集合，查询商品分类数据自动封装成list集合
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids) {
        List<String> list = this.categoryService.queryNamesByCids(ids);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 根据3级分类id，查询1~3级的分类
     *
     * @param id
     * @return
     */
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id) {
        List<Category> list = this.categoryService.queryAllByCid3(id);
        if (list == null || list.size() < 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 查询所有的二三级分类信息
     */
    @GetMapping("allList")
    public ResponseEntity<Map<String, List<Category>>> queryAllList(@RequestParam("oneId") Long id) {
        //声明一个Map集合，来存放二三级分类信息
        Map<String,List<Category>> map = new HashMap<>();
        //先查询出二级分类列表
        List<Category> twoList = this.categoryService.queryListByParent(id);
        //遍历二级分类 通过id查询三级分类信息
        for (int i = 0; i < twoList.size(); i++) {
            //二级分类的id
            Long twoId = twoList.get(i).getId();
            List<Category> threeList = this.categoryService.queryListByParent(twoId);
            //将相对应的二级分类名称和三级分类信息存放到map集合中
            map.put(twoList.get(i).getName(),threeList);
        }
        if (map == null || map.size() < 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(map);
    }
}
